'use strict';

import IApp from './IApp';
import * as PIXI from 'pixi.js';

class BaseApp implements IApp {
  protected element: HTMLCanvasElement = null;
  protected app: PIXI.Application = null;

  public create() {
    this.destory();
    const can = document.createElement('canvas');
    this.element = can;
    window.onresize = (e) => {
      this.onresize();
    };

    return can;
  }

  public start() {}

  public onresize(): any | void {
    if (this.app) {
      this.app.resize();
    }
  }

  public destory() {
    if (this.app) {
      this.app.destroy(true, {
        children: true,
        texture: true,
        baseTexture: true,
      });
      this.app = null;
    }
    if (this.element !== null) {
      const content = document.getElementById('content');
      if (content && content.children[0] == this.element) {
        content.removeChild(this.element);
      }
      this.element = null;
    }
  }
}

export default BaseApp;
